import 'package:flutter/material.dart';
import '../../tokens/colors.dart';
import '../../tokens/spacing.dart';
import '../../tokens/typography.dart';
import '../../tokens/borders.dart';

/// UI组件库按钮样式
/// 提供统一的按钮样式配置
class UIButtonStyles {
  // 按钮尺寸
  static const double smallHeight = 32.0;
  static const double mediumHeight = 40.0;
  static const double largeHeight = 48.0;

  static const double smallMinWidth = 64.0;
  static const double mediumMinWidth = 88.0;
  static const double largeMinWidth = 120.0;

  // Primary按钮样式
  static ButtonStyle primaryButton(ColorScheme colorScheme) {
    return ElevatedButton.styleFrom(
      backgroundColor: colorScheme.primary,
      foregroundColor: colorScheme.onPrimary,
      elevation: 1,
      shadowColor: colorScheme.shadow,
      shape: RoundedRectangleBorder(
        borderRadius: UIBorders.button,
      ),
      padding: const EdgeInsets.symmetric(
        horizontal: UISpacing.buttonPadding,
        vertical: UISpacing.buttonPaddingSmall,
      ),
      minimumSize: const Size(mediumMinWidth, mediumHeight),
      textStyle: UITextStyles.button,
    ).copyWith(
      overlayColor: WidgetStateProperty.resolveWith<Color?>((states) {
        if (states.contains(WidgetState.hovered)) {
          return colorScheme.onPrimary.withOpacity(0.08);
        }
        if (states.contains(WidgetState.pressed)) {
          return colorScheme.onPrimary.withOpacity(0.12);
        }
        return null;
      }),
      elevation: WidgetStateProperty.resolveWith<double>((states) {
        if (states.contains(WidgetState.pressed)) {
          return 0;
        }
        if (states.contains(WidgetState.hovered)) {
          return 2;
        }
        return 1;
      }),
    );
  }

  // Secondary按钮样式
  static ButtonStyle secondaryButton(ColorScheme colorScheme) {
    return OutlinedButton.styleFrom(
      foregroundColor: colorScheme.primary,
      side: BorderSide(
        color: colorScheme.outline,
        width: UIBorders.widthMedium,
      ),
      shape: RoundedRectangleBorder(
        borderRadius: UIBorders.button,
      ),
      padding: const EdgeInsets.symmetric(
        horizontal: UISpacing.buttonPadding,
        vertical: UISpacing.buttonPaddingSmall,
      ),
      minimumSize: const Size(mediumMinWidth, mediumHeight),
      textStyle: UITextStyles.button,
    ).copyWith(
      overlayColor: WidgetStateProperty.resolveWith<Color?>((states) {
        if (states.contains(WidgetState.hovered)) {
          return colorScheme.primary.withOpacity(0.04);
        }
        if (states.contains(WidgetState.pressed)) {
          return colorScheme.primary.withOpacity(0.08);
        }
        return null;
      }),
    );
  }

  // 文本按钮样式
  static ButtonStyle textButton(ColorScheme colorScheme) {
    return TextButton.styleFrom(
      foregroundColor: colorScheme.primary,
      shape: RoundedRectangleBorder(
        borderRadius: UIBorders.button,
      ),
      padding: const EdgeInsets.symmetric(
        horizontal: UISpacing.buttonPaddingSmall,
        vertical: UISpacing.buttonPaddingSmall,
      ),
      minimumSize: const Size(mediumMinWidth, mediumHeight),
      textStyle: UITextStyles.button,
    );
  }

  // 成功按钮样式
  static ButtonStyle successButton(ColorScheme colorScheme) {
    return ElevatedButton.styleFrom(
      backgroundColor: UIColors.success,
      foregroundColor: UIColors.onSuccessContainer,
      elevation: 1,
      shape: RoundedRectangleBorder(
        borderRadius: UIBorders.button,
      ),
      padding: const EdgeInsets.symmetric(
        horizontal: UISpacing.buttonPadding,
        vertical: UISpacing.buttonPaddingSmall,
      ),
      minimumSize: const Size(mediumMinWidth, mediumHeight),
      textStyle: UITextStyles.button,
    );
  }

  // 错误按钮样式
  static ButtonStyle errorButton(ColorScheme colorScheme) {
    return ElevatedButton.styleFrom(
      backgroundColor: colorScheme.error,
      foregroundColor: colorScheme.onError,
      elevation: 1,
      shape: RoundedRectangleBorder(
        borderRadius: UIBorders.button,
      ),
      padding: const EdgeInsets.symmetric(
        horizontal: UISpacing.buttonPadding,
        vertical: UISpacing.buttonPaddingSmall,
      ),
      minimumSize: const Size(mediumMinWidth, mediumHeight),
      textStyle: UITextStyles.button,
    );
  }

  // 警告按钮样式
  static ButtonStyle warningButton(ColorScheme colorScheme) {
    return ElevatedButton.styleFrom(
      backgroundColor: UIColors.warning,
      foregroundColor: UIColors.onWarningContainer,
      elevation: 1,
      shape: RoundedRectangleBorder(
        borderRadius: UIBorders.button,
      ),
      padding: const EdgeInsets.symmetric(
        horizontal: UISpacing.buttonPadding,
        vertical: UISpacing.buttonPaddingSmall,
      ),
      minimumSize: const Size(mediumMinWidth, mediumHeight),
      textStyle: UITextStyles.button,
    );
  }

  // 小尺寸按钮样式
  static ButtonStyle smallButton(ButtonStyle baseStyle) {
    return baseStyle.copyWith(
      minimumSize: WidgetStateProperty.all(const Size(smallMinWidth, smallHeight)),
      padding: WidgetStateProperty.all(const EdgeInsets.symmetric(
        horizontal: UISpacing.buttonPaddingSmall,
        vertical: UISpacing.xs,
      )),
      shape: WidgetStateProperty.all(RoundedRectangleBorder(
        borderRadius: UIBorders.buttonSmall,
      )),
      textStyle: WidgetStateProperty.all(UITextStyles.buttonSmall),
    );
  }

  // 大尺寸按钮样式
  static ButtonStyle largeButton(ButtonStyle baseStyle) {
    return baseStyle.copyWith(
      minimumSize: WidgetStateProperty.all(const Size(largeMinWidth, largeHeight)),
      padding: WidgetStateProperty.all(const EdgeInsets.symmetric(
        horizontal: UISpacing.buttonPaddingLarge,
        vertical: UISpacing.buttonPaddingSmall,
      )),
      shape: WidgetStateProperty.all(RoundedRectangleBorder(
        borderRadius: UIBorders.buttonLarge,
      )),
    );
  }

  // 加载状态按钮样式
  static ButtonStyle loadingButton(ColorScheme colorScheme) {
    return primaryButton(colorScheme).copyWith(
      backgroundColor: WidgetStateProperty.all(
        colorScheme.primary.withOpacity(0.6),
      ),
      foregroundColor: WidgetStateProperty.all(
        colorScheme.onPrimary.withOpacity(0.6),
      ),
    );
  }

  // 禁用状态按钮样式
  static ButtonStyle disabledButton(ColorScheme colorScheme) {
    return primaryButton(colorScheme).copyWith(
      backgroundColor: WidgetStateProperty.all(
        colorScheme.onSurface.withOpacity(0.12),
      ),
      foregroundColor: WidgetStateProperty.all(
        colorScheme.onSurface.withOpacity(0.38),
      ),
      elevation: WidgetStateProperty.all(0),
    );
  }

  // 圆形按钮样式
  static ButtonStyle circularButton(ColorScheme colorScheme, {double size = 48.0}) {
    return primaryButton(colorScheme).copyWith(
      shape: WidgetStateProperty.all(const CircleBorder()),
      minimumSize: WidgetStateProperty.all(Size(size, size)),
      padding: WidgetStateProperty.all(EdgeInsets.zero),
    );
  }

  // 图标按钮样式
  static ButtonStyle iconButton(ColorScheme colorScheme) {
    return IconButton.styleFrom(
      foregroundColor: colorScheme.onSurface,
      backgroundColor: const Color(0x00000000), // 透明色
      shape: RoundedRectangleBorder(
        borderRadius: UIBorders.sm,
      ),
      padding: const EdgeInsets.all(UISpacing.sm),
      minimumSize: const Size(40, 40),
    );
  }

  // 浮动操作按钮样式
  static ButtonStyle floatingActionButton(ColorScheme colorScheme) {
    return ElevatedButton.styleFrom(
      backgroundColor: colorScheme.primaryContainer,
      foregroundColor: colorScheme.onPrimaryContainer,
      elevation: 3,
      shape: RoundedRectangleBorder(
        borderRadius: UIBorders.xl,
      ),
    );
  }

  // 分段按钮样式
  static ButtonStyle segmentedButton(ColorScheme colorScheme, {bool isSelected = false}) {
    return OutlinedButton.styleFrom(
      backgroundColor: isSelected ? colorScheme.secondaryContainer : const Color(0x00000000),
      foregroundColor: isSelected ? colorScheme.onSecondaryContainer : colorScheme.onSurface,
      side: BorderSide(
        color: colorScheme.outline,
        width: UIBorders.widthMedium,
      ),
      shape: const RoundedRectangleBorder(),
      padding: const EdgeInsets.symmetric(
        horizontal: UISpacing.md,
        vertical: UISpacing.sm,
      ),
      minimumSize: const Size(0, 40),
      textStyle: UITextStyles.labelLarge,
    );
  }

  // 切换按钮样式
  static ButtonStyle toggleButton(ColorScheme colorScheme, {bool isSelected = false}) {
    return OutlinedButton.styleFrom(
      backgroundColor: isSelected ? colorScheme.primaryContainer : const Color(0x00000000),
      foregroundColor: isSelected ? colorScheme.onPrimaryContainer : colorScheme.primary,
      side: BorderSide(
        color: isSelected ? colorScheme.primary : colorScheme.outline,
        width: UIBorders.widthMedium,
      ),
      shape: RoundedRectangleBorder(
        borderRadius: UIBorders.button,
      ),
      padding: const EdgeInsets.symmetric(
        horizontal: UISpacing.md,
        vertical: UISpacing.sm,
      ),
      textStyle: UITextStyles.button,
    );
  }

  // 获取按钮样式工厂方法
  static ButtonStyle getButtonStyle({
    required String type,
    required ColorScheme colorScheme,
    String size = 'medium',
    bool isSelected = false,
  }) {
    ButtonStyle baseStyle;

    switch (type.toLowerCase()) {
      case 'primary':
        baseStyle = primaryButton(colorScheme);
        break;
      case 'secondary':
        baseStyle = secondaryButton(colorScheme);
        break;
      case 'text':
        baseStyle = textButton(colorScheme);
        break;
      case 'success':
        baseStyle = successButton(colorScheme);
        break;
      case 'error':
        baseStyle = errorButton(colorScheme);
        break;
      case 'warning':
        baseStyle = warningButton(colorScheme);
        break;
      case 'toggle':
        return toggleButton(colorScheme, isSelected: isSelected);
      case 'segmented':
        return segmentedButton(colorScheme, isSelected: isSelected);
      case 'icon':
        return iconButton(colorScheme);
      case 'fab':
        return floatingActionButton(colorScheme);
      default:
        baseStyle = primaryButton(colorScheme);
    }

    switch (size.toLowerCase()) {
      case 'small':
        return smallButton(baseStyle);
      case 'large':
        return largeButton(baseStyle);
      default:
        return baseStyle;
    }
  }
}